$(function () {
    $.get("/dictionaryItems/getItemByDictionarySn.do?sn=operation", function (data) {
        op = data;
    })
    $.get("/dictionaryItems/getItemByDictionarySn.do?sn=zhifu", function (data) {
        pay = data;
    })
    var recharge_datagrid = $("#recharge_datagrid");
    var recharge_dialog = $("#recharge_dialog");
    var editForm = $("#editForm");
    var rechargeForm = $("#rechargeForm");
    recharge_dialog.dialog({
        width: 900,
        height: 600,
        buttons: "#bb",
        closable: true,
        closed: true
    })
    //列表初始化
    recharge_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/recharge/list.do",
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: "#tb",
        columns: [[
            {field: "cb", checkbox: true},
            {field: "id", hidden: true},
            {field: "card_num", title: "卡号", width: 80},
            {field: "op_id", title: "操作类型", width: 80, formatter: opformatter},
            {field: "pay_id", title: "支付方式", width: 80, formatter: payformatter},
            {field: "oldbalance", title: "变动前余额", width: 80},
            {field: "addmoney", title: "充值金额", width: 80},
            {field: "givemoney", title: "赠送金额", width: 80},
            {field: "newbalance", title: "变动后余额", width: 80},
            {field: "rechargetime", title: "操作时间", width: 80}
        ]]
    })
    //end
    var cmdObj = {
        //充值添加
        add: function () {
            //清除缓存数据
            editForm.form("clear");
            rechargeForm.form("clear");
            $("#empId").val("");
            //打开弹出框
            recharge_dialog.dialog("setTitle", "会员充值");
            recharge_dialog.dialog("open");
        },
        //对话框取消
        cancel: function () {
            //关闭窗口
            recharge_dialog.dialog("close");
        },
        //----------------------------------|
        //充值保存/更新
        save: function () {
            //表单提交url
            //使用id区分保存于更新操作
            var id = $("#empId").val();
            if (!id) {
                $.messager.alert('温馨提示', '请选择要操作的会员后执行');
                return;
            }
            url = id ? "/member/edit.do" : "/member/save.do";
            //提交表单
            editForm.form("submit", {
                url: url,
                success: function (data) {
                    //jquery将字符串转换json对象
                    //data必须是标准的json字符串
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示成功
                        $.messager.confirm('温馨提示', "操作成功", function (r) {
                            //关闭窗口
                            member_dialog.dialog("close");
                            //刷新数据
                            member_datagrid.datagrid("reload");

                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, "error");
                    }
                }

            })
        },
        //会员查询
        searchT: function () {
            var info = $("#keywordinfo").val();
            $.get("/member/getMemByKeyInfo.do?keyInfo=" + info, function (data) {
                editForm.form("load", data);
            })
            $("#memNum").val($("#cardNum").val());
        },
        //刷新
        reload: function () {
            recharge_datagrid.datagrid("reload");
        }
    }
    //给按钮绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
})
var op;
var pay;

function opformatter(value, row, index) {
    return $.map(op, function (n, i) {
        if (n.id == value) {
            return n.name;
        }
    });
}

function payformatter(value, row, index) {
    return $.map(pay, function (n, i) {
        if (n.id == value) {
            return n.name;
        }
    });

}















